package com.gzhryc.shared_device.oem.h5.mng.controllers.config;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.common.payment.dao.db.AlipayConfig;
import com.gzhryc.shared_device.common.payment.dao.db.AppConfig;
import com.gzhryc.shared_device.common.payment.dao.enums.AlipayConfigEnum;
import com.gzhryc.shared_device.common.payment.dao.enums.EAppType;
import com.gzhryc.shared_device.common.payment.services.AlipayConfigService;
import com.gzhryc.shared_device.common.payment.services.AppConfigService;
import com.gzhryc.shared_device.common.payment.services.dto.AlipayConfigSearch;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

public class AlipayConfigController {

	@Route(value = { "/config/alipay/list.html" })
	public String list(HttpServletRequest request) {
		request.setAttribute("appTypeList", AlipayConfigEnum.AppType.values());
		return "/config/alipay/list.html";
	}

	@Route(value = "/config/alipay/list.data")
	public ListResult<AlipayConfig> listData(HttpServletRequest request) {
		ListResult<AlipayConfig> result = new ListResult<>(DtoConstants.NULL_DATA,"暂无数据");

		AlipayConfigSearch search = WebUtils.getParameter(request, AlipayConfigSearch.class);
		Integer pageNo = WebUtils.getInteger(request, "page", 1);
		Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

		result.getBody().setTotal(AlipayConfigService.self().countJoinBySearch(search));
		result.getBody().setData(AlipayConfigService.self().findJoinBySearch(search, pageNo, pageLimit));

		if (result.getBody().getData() != null && result.getBody().getData().size() > 0) {
			result.setCode(DtoConstants.SUCCESS, null);
		}

		return result;
	}

	@Route(value = { "/config/alipay/add.html" })
	public String add(HttpServletRequest request) {
		request.setAttribute("appTypeList", AlipayConfigEnum.AppType.values());
		return "/config/alipay/edit.html";
	}
	
	@Route(value = { "/config/alipay/add.do" })
	public AjaxResult doAdd(HttpServletRequest request) {
		AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"新增失败");
		AlipayConfig alipayConfig = WebUtils.getParameter(request, AlipayConfig.class);
		try {
			if(AlipayConfigService.self().add(alipayConfig)) {
				result.setCode(DtoConstants.SUCCESS, null);
			}
		} catch (LogicException e) {
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@Route(value = { "/config/alipay/update.html" })
	public String update(HttpServletRequest request) {
		String appId = WebUtils.getString(request, "id");
		if(StringTools.isNotBlank(appId)) {
			AlipayConfig alipayConfig = AlipayConfigService.self().getByAppId(appId);
			if(alipayConfig != null) {
				WebUtils.setAttribute(request, alipayConfig);
			}
		}
		request.setAttribute("appTypeList", AlipayConfigEnum.AppType.values());
		return "/config/alipay/edit.html";
	}
	
	@Route(value = { "/config/alipay/update.do" })
	public AjaxResult doUpdate(HttpServletRequest request) {
		AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"修改失败");
		AlipayConfig alipayConfig = WebUtils.getParameter(request, AlipayConfig.class);
		try {
			if(AlipayConfigService.self().updateNotNull(alipayConfig)) {
				result.setCode(DtoConstants.SUCCESS, null);
			}
		} catch (LogicException e) {
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@Route(value = { "/config/alipay/busi_conf/update.html" })
	public String updateBusinessConfig(HttpServletRequest request) {
		String appId = WebUtils.getString(request, "appId");
		if(StringUtils.isNotBlank(appId)) {
			AppConfig appConfig = AppConfigService.self().getByKeys(appId, EAppType.Aliyun.index());
			WebUtils.setAttribute(request,appConfig);
		}
		return "/config/alipay/business.html";
	}

	@Route(value = { "/config/alipay/busi_conf/save.do" })
	public AjaxResult doBusinessConfigUpdate(HttpServletRequest request) {
		AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"修改失败");
		AppConfig appConfig = WebUtils.getParameter(request, AppConfig.class);
		try {
			appConfig.setType(EAppType.Aliyun.index());
			if(AppConfigService.self().save(appConfig)){
				result.setCode(DtoConstants.SUCCESS,null);
			}
		} catch (LogicException e) {
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@Route(value = { "/config/alipay/del.do" })
	public AjaxResult doDelete(HttpServletRequest request) {
		AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"删除失败，请稍后再试");
		String appId = WebUtils.getString(request, "id");
		try {
			if(AlipayConfigService.self().deleteByAppId(appId)) {
				result.setCode(DtoConstants.SUCCESS, null);
			}
		} catch (LogicException e) {
			result.setMsg(e.getMessage());
		}
		return result;
	}
}
